function UserEditCtrl($rootScope,$scope,$http,$timeout){
	
	$scope.userId = 0;
	$scope.email = "";
	$scope.phoneNumber = "";
	$scope.password = "";
	$scope.name = "";
	$scope.changePassword = false;
	
	$scope.userAction = "הוסף";
	$scope.actionUrl = "/saftalk/adduser";
	$scope.isEdit = false;
	
	
	$scope.submitButton = new Object();
	initSubmitButton=function () {
	
	$scope.submitButton.label="שמור";
	$scope.submitButton.disabled="";
	}
	disableSubmitButton=function () {
		$scope.submitButton.label="המתן...";
		$scope.submitButton.disabled="disabled";
	}
	initSubmitButton();
	
	
	$scope.$on('initUserEdit', function(event,argsObj) {
		console.log("logging initUserEdit:");
		console.log(argsObj);
		console.log(argsObj.userObj);
		$scope.name = argsObj.userObj.name;
		$scope.email = argsObj.userObj.email;
		$scope.phoneNumber = argsObj.userObj.phoneNumber;
		$scope.userId = argsObj.userObj.userId;
		$scope.password = argsObj.userObj.generatedPassword;
		$scope.changePassword=false;
		$scope.userAction = "ערוך";
		$scope.actionUrl = "/saftalk/user/edit";
		$scope.isEdit = true;
		initSubmitButton();
		
	});
	
	$scope.$on('initUserAdd', function(event,argsObj) {
		console.log("logging initUserAdd:");
		console.log(argsObj);
		console.log(argsObj.userObj);
		$scope.name = null;
		$scope.email = null;
		$scope.phoneNumber = null;
		$scope.userId = null;
		$scope.password = null;
		$scope.changePassword=false;
		$scope.userAction = "הוסף";
		$scope.actionUrl = "/saftalk/adduser";
		$scope.isEdit = false;
		initSubmitButton();

	});
	
	
	
	$scope.saveUser = function() {
		disableSubmitButton();
		console.log("changepass:" + $scope.changePassword );
		var myParams = {name: $scope.name, email: $scope.email, phoneNumber: $scope.phoneNumber};
		
		if ($scope.isEdit) {
			$.extend(myParams,  {userId: $scope.userId});
			if ($scope.changePassword) {
				$.extend(myParams, {changePassword: $scope.changePassword, password: $scope.password});
			}		
		}
		
		$http({
			method: 'POST',
			url: $scope.actionUrl, 
			data: $.param(myParams),
			headers: {'Content-Type': 'application/x-www-form-urlencoded'}
			}).success(function(data) {
            if (!data.success) {            	
            	console.log("Failed to edit or add user");
				$(".login-alerts .alert-error").hide();
				$(".login-alerts .alert-error").removeClass("hidden");
				$(".login-alerts .alert-error").fadeIn();
				initSubmitButton();
                return; 
            }
            $rootScope.$broadcast('finishUserEditOrAdd', {userObj: data} );
		});
		
	}
	
	$scope.cancelUser = function() {
		$('#userEditPopUp').bPopup().close();
	}
	
}